/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.base.controller;

import com.xy.biz.base.client.BomClient;
import com.xy.biz.base.client.DictClient;
import com.xy.biz.base.domain.converter.BomConverter;
import com.xy.biz.base.domain.query.BomQuery;
import com.xy.biz.base.domain.req.GetBomRequest;
import com.xy.biz.base.domain.req.ListBomRequest;
import com.xy.biz.base.domain.req.ListDictItemRequest;
import com.xy.biz.base.domain.req.SaveBomRequest;
import com.xy.biz.base.domain.resp.GetBomResponse;
import com.xy.biz.base.domain.resp.ListDictItemResponse;
import com.xy.biz.base.service.BomService;
import com.xy.lang.domain.KeyValue;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.enums.ApplyStatusEnum;
import com.xy.lang.enums.AuditStatusEnum;
import com.xy.lang.util.ResultUtils;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 *
 *
 * @author zsp
 * @date 2021-12-28
 */
@Slf4j
@Api(tags = "物料清单服务")
@RestController
@RequestMapping("/api/base/bom")
public class BomController implements BomClient {

    @Autowired
    private BomService bomService;
    @Autowired
    private DictClient dictClient;

    @ApiOperation("新增")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> saveBom(@RequestBody SaveBomRequest request) {
        return bomService.saveBom(BomConverter.toBO(request));
    }

    @ApiOperation("编辑")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updateBom(@RequestBody SaveBomRequest request) {
        return bomService.updateBom(BomConverter.toBO(request));
    }

    @ApiOperation("复制")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/copy")
    public Result<?> copyBom(@RequestBody GetBomRequest request) {
        return bomService.copyBom(request.getBomId());
    }

    @ApiOperation("审核")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/audit")
    public Result<?> auditBom(@RequestBody SaveBomRequest request) {
        return bomService.auditBom(BomConverter.toBO(request));
    }

//    @ApiOperation("申请")
//    @Validatable
//    @PostMapping(value = "/apply")
//    public Result<?> applyAuditFlow(@RequestBody ApplyAuditFlowRequest request) {
//        return auditCommonFlowService.applyAuditFlow(request);
//    }
//
//    @ApiOperation("审核")
//    @Validatable
//    @PostMapping(value = "/audit")
//    public Result<?> auditAuditFlow(@RequestBody AuditFlowRequest request) {
//        return auditCommonFlowService.auditAuditFlow(request);
//    }
//
//    @ApiOperation("审批")
//    @Validatable
//    @PostMapping(value = "/approve")
//    public Result<?> approveAuditFlow(@RequestBody AuditFlowRequest request) {
//        return auditCommonFlowService.approveAuditFlow(request);
//    }

    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteBom(@RequestBody GetBomRequest request) {
        return bomService.deleteBom(request.getBomId());
    }

    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<GetBomResponse> getBom(@RequestBody GetBomRequest request) {
        return Result.success(BomConverter.toResponse(bomService.getBom(BomConverter.toQuery(request))));
    }

    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<GetBomResponse>> listBomById(@RequestBody Collection<Long> collection) {
        return Result.success(BomConverter.toResponse(bomService.listBomById(collection)));
    }

    @PostMapping(value = "/list")
    @Override
    public Result<List<GetBomResponse>> listBom(@RequestBody ListBomRequest request) {
        return Result.success(BomConverter.toResponse(bomService.listBom(BomConverter.toQuery(request))));
    }

    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetBomResponse>> pageBom(@RequestBody ListBomRequest request) {
        BomQuery query = BomConverter.toQuery(request);
        List<GetBomResponse> list = BomConverter.toResponse(bomService.listBom(query));
        int count = bomService.countBom(query);
        return Result.success(new Page<>(list, count));
    }

    @GetMapping(value = "/list_pre_options")
    public Result<Map<String, List>> listPreOptions() {
        Map<String, List> map = new HashMap<>();
        map.put("status", listStatus());
        map.put("auditStatus", listStatus1());
        List<ListDictItemResponse> dictList = ResultUtils.get(dictClient.listDictItem(
                ListDictItemRequest.builder().pageSize(1000).dictGroupCodes(
                        Arrays.asList("goods_unit")).build()));
        if (!CollectionUtils.isEmpty(dictList)) {
            List<KeyValue<String, String>> customerCategoryList = new ArrayList<>();
            dictList.forEach(e -> {
                if("goods_unit".equals(e.getDictGroupCode())) {
                    customerCategoryList.add(new KeyValue<>(e.getDictValue(), e.getDictValue()));
                }
            });
            map.put("goodsUnit", customerCategoryList);
        }
        return Result.success(map);
    }

    private List<KeyValue<Integer, String>> listStatus() {
        ApplyStatusEnum[] enums = ApplyStatusEnum.values();
        List<KeyValue<Integer, String>> list = new ArrayList<>();
        for (ApplyStatusEnum e : enums) {
            list.add(e.toKeyValue());
        }
        return list;
    }

    private List<KeyValue<Integer, String>> listStatus1() {
        AuditStatusEnum[] enums = AuditStatusEnum.values();
        List<KeyValue<Integer, String>> list = new ArrayList<>();
        for (AuditStatusEnum e : enums) {
            list.add(e.toKeyValue());
        }
        return list;
    }
}
